package com.dmall.play.interview.chapter08;

import java.util.ArrayList;
import java.util.List;

public class Leetcode17V2 {

    private final static char[][] LETTER_MAP = {
            {' '},
            {},
            {'a', 'b', 'c'},
            {'d', 'e', 'f'},
            {'g', 'h', 'i'},
            {'j', 'k', 'l'},
            {'m', 'n', 'o'},
            {'p', 'q', 'r', 's'},
            {'t', 'u', 'v'},
            {'w', 'x', 'y', 'z'}
    };

    private final List<String> res = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || "".equalsIgnoreCase(digits)) {
            return res;
        }
        findCombinations(digits, 0, "");
        return res;
    }

    private void findCombinations(String digits, int index, String digit) {
        if (index == digits.length()) {
            res.add(digit);
            return;
        }
        char c = digits.charAt(index);
        char[] chars = LETTER_MAP[c - '0'];
        for (int i = 0; i < chars.length; i++) {
            findCombinations(digits, index + 1, digit + chars[i]);
        }
    }

    public static void main(String[] args) {
        Leetcode17V2 leetcode17 = new Leetcode17V2();
        List<String> strings = leetcode17.letterCombinations("23");
        System.out.println(strings);
    }
}
